<template>
  <div class="derivedTbale">
    <!-- 按钮组件导入、导出、下载模板 -->
    <el-button icon="el-icon-download" type="success" plain @click="exportXlsx" size="mini" :disabled="disableds">导出Excel</el-button>
  </div>
</template>

<script>
export default {
  name: "derivedTbale",
  props: {
    suoS: {
      type: Object,
      default: () => ({}),
    },
    multipleSelection: {
      type: Array,
      default: () => [],
    },
    importUrl: {
      type: String,
      default: "",
    },
    commonPrint: {
      type: Boolean,
      default: false,
    },
    disabledInit: {
      type: Function,
      default: () => { },
    },
    disabled: {
      type: Boolean,
      default: false,
    },
  },
  watch: {
    disabled (newValue) {
      this.disableds = newValue;
    },
  },
  data () {
    return {
      fileList: [],
      disableds: this.disabled,
      disabledPrint: false,
      disableExportData: false,
    };
  },
  methods: {
    // 打印
    printDom () {
      this.$emit("disabledInit");
    },
    // 弹窗关闭
    handleClose (done) {
      this.$confirm("确认关闭？")
        .then(() => {
          done();
        })
        .catch(() => { });
    },
    // 导出方法
    async exportXlsx () {
      this.disableds = true;
      console.log(123);

      try {
        const { exprotXlsx } = this.$parent;
        for (let item in this.suoS) {
          if (
            Array.isArray(this.suoS[item]) &&
            [
              "book",
              "hangbookCode",
              "bookName",
              "meterReadingManual",
              "name",
              "bookId",
            ].includes(item)
          ) {
            this.suoS[item] = this.suoS[item].join(",");
          }
        }
        const suoSs = this.suoS;
        let multipleSelections = this.multipleSelection;
        console.log(multipleSelections, 999);
        let ids = [];
        for (let i = 0; i < multipleSelections.length; i++) {
          const element = multipleSelections[i];
          ids.push(element.id);
        }

        let res = ids.length
          ? await exprotXlsx(ids, suoSs)
          : await exprotXlsx("ids", suoSs);
        console.log(res, "导出结果");
        let fileName = decodeURI(res["filename"].split("=")[1]);

        let blob = new Blob([res], {
          type: "application/vnd.ms-excel;charset=utf-8",
        });
        var downloadA = document.createElement("a");
        downloadA.download = fileName;
        downloadA.style.display = "none";
        downloadA.href = URL.createObjectURL(blob);
        document.body.appendChild(downloadA);
        downloadA.click();
        URL.revokeObjectURL(downloadA.href);
        document.body.removeChild(downloadA);
        // 提示
        this.$notify({
          title: "导出成功",
          message: fileName,
          type: "success",
        });
      } catch (error) {
        console.log(error);
        this.$notify.error({
          title: "错误",
          message: "下载失败！",
        });
      } finally {
        this.disableds = false;
      }
    },
  },
  created () { },
};
</script>

<style lang="scss" scoped>
.derivedTbale {
  display: inline-block;
}
</style>
